import BSTMap from '../../../src/datastructure/map/BSTMap'
import AVLTreeMap from '../../../src/datastructure/map/AVLTreeMap'
import fs from 'fs'
async function readFile (url) {
  return new Promise((resolve, reject) => {
    fs.readFile('pride-and-prejudice.txt', function (err, data) {
      if (err) {
        reject(err)
      }
      resolve(data.toString())
    })
  })
}
async function getWords () {
  const data = await readFile('pride-and-prejudice.txt')
  const re = /\w+\b/g
  const arr = data.match(re).map(value => value.toLowerCase())
  return arr
}
async function testMap (map, label) {
  const arr = await getWords()
  console.time(label)
  for (const word of arr) {
    if (map.contains(word)) {
      map.set(word, map.get(word) + 1)
    } else {
      map.add(word, 1)
    }
  }
  console.log('mapSize: ', map.getSize())
  for (const word of arr) {
    map.contains(word)
  }
  console.timeEnd(label)
}

const map = new BSTMap()
const avlMap = new AVLTreeMap()

testMap(map, 'BSTMap')
testMap(avlMap, 'AVLMap')
